package com.situ.web.service.impl;

import java.util.List;

import com.situ.web.dao.IStudentDao;
import com.situ.web.dao.impl.StudentDaoImpl;
import com.situ.web.entity.BanJi;
import com.situ.web.entity.PageBean;
import com.situ.web.entity.Student;
import com.situ.web.exception.NameAgainException;
import com.situ.web.service.IStudentService;

public class StudentServiceImpl implements IStudentService{
	private IStudentDao studentDao = new StudentDaoImpl();

	@Override
	public PageBean findPageBean(int pageNo, int pageSize) {
		PageBean pageBean = new PageBean();
		pageBean.setPageNo(pageNo);
		pageBean.setPageSize(pageSize);
		int count = 0;
		count = studentDao.allCount();
		int totalPage = (int)Math.ceil((double)count/pageSize);
		pageBean.setTotalPage(totalPage);
		int offset = (pageNo - 1)*pageSize;
		List<Student> list = studentDao.findList(offset,pageSize);
		pageBean.setList(list);
		return pageBean;
	}

	@Override
	public boolean add(Student student) throws NameAgainException {
		boolean result = false;
		if (studentDao.checkName(student.getName())) {
			throw new NameAgainException("已存在该姓名");
		}else {
			result = studentDao.add(student);
		}
		return result;
	}

	@Override
	public boolean deleteById(int stuId) {
		return studentDao.deleteById(stuId);
	}

	@Override
	public Student toUpdate(int id) {
		return studentDao.toUpdate(id);
	}

	@Override
	public boolean update(Student student) {
		return studentDao.update(student);
	}

	@Override
	public List<BanJi> findAllBanJi() {
		return studentDao.findAllBanJi();
	}



}
